$.ajaxSetup({
    headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    }
});
var templateEvent = new Event('appStart');

var templateList = {
  'template?name=auth.register': AuthRegisterView, 
  'template?name=auth.login': AuthLoginView,
  'template?name=users.profile': UserProfileView,
  'template?name=auth.active': AuthActiveView,
};

function loadTemplates(templateList) {
  var i = 0;
  var templates = []; 
  var views = [];
  for (var j in templateList) {
    templates.push(j);
    views.push(templateList[j]);
  }
  
  var loadTemplate = function() {
    if (!templates[i]) {
      document.body.dispatchEvent(templateEvent);
      return $.when(1);
    }

    if (views[i].prototype.template) {
      i++;
      return loadTemplate();
    }

    return $.get(templates[i]).then(function(data) {
      views[i].prototype.template = _.template(data);
      i++;
      return loadTemplate();
    }, function(data) {
      i++;
      return loadTemplate();
    });
  };

  return loadTemplate();
}

function redirect(url) {
  var currentUrl = location.href.replace(/\?.*/, '');
  currentUrl = currentUrl.replace(/\/$/, '');
  if (currentUrl != url) {
    location.href = url;
  }
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function toast(message, type) {
  // var data = {message: message, timeout: 3000};
  // var snackbarContainer = $('.mdl-js-snackbar');
  // snackbarContainer.removeClass('mdl-color--red-900');
  // snackbarContainer.removeClass('mdl-color--green-900');
  //
  // if (type == 'alert') {
  //   snackbarContainer.addClass('mdl-color--red-900');
  // } else if (type == 'success') {
  //   snackbarContainer.addClass('mdl-color--green-900');
  // }
  // snackbarContainer[0].MaterialSnackbar.showSnackbar(data);
  var notification = $('.notification');
  notification.empty();
  notification.html(message);

}

function showDialog(i) {
  i = i || 0;
  var dialog = $('dialog')[i];
  if (dialog && !dialog.showModal) {
    dialogPolyfill.registerDialog(dialog);
  }
  dialog.showModal();
  var closeBtns = dialog.querySelectorAll('.close');
  for (var j = 0, l = closeBtns.length; j < l; j++) {
    closeBtns[j].addEventListener('click', function() {
      dialog.close();
    });
  }
  return dialog;
}
// close Dialog function
function hideDialog(i){
  i = i || 0;
  var dialog = $('dialog')[i];
  if (dialog && !dialog.showModal) {
    dialogPolyfill.registerDialog(dialog);
  }
  dialog.close();
}

var promises = loadTemplates(templateList);

document.body.addEventListener('appStart', function(e) {
  promises.then(function() {
    window.currentUser = null;
    window.router = new MemberRouter();
    if(!Backbone.History.started) {
      Backbone.history.start();
    }

    //fix wechat first load issue
    window.setTimeout(function() {
      if (!Backbone.history.fragment) {
        window.router.navigateTo('profile');
      }
    }, 600);
    
  });
}, false);